package iofile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @BelongsProject: java-study
 * @BelongsPackage: iofile
 * @Author: gejingyu
 * @Date: 2023/2/8
 * @Description:
 */

public class ObjectOutputStream_ {

    public static void main(String[] args) {

        //序列化后，保存的文件格式不是纯文本，而是按照对象的数据格式来保存，文件的后缀名没有实际意义
        String filePath = "";
        ObjectOutputStream objectOutputStream = null;

        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));
            //底层将int封装为Integer，Integer实现了Serializable
            objectOutputStream.writeInt(100);
            //底层将boolean封装为Boolean，Boolean实现了Serializable
            objectOutputStream.writeBoolean(true);
            //底层将char封装为Character，Character实现了Serializable
            objectOutputStream.writeChar('a');
            //底层将double封装为Double，Double实现了Serializable
            objectOutputStream.writeDouble(9.5);
            //String本身实现了Serializable
            objectOutputStream.writeUTF("测试");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
